let domain = "https://api.jiajiedaojia.com";
// let domain = "http://jiajie.com/";

let api = {
	login: function(params,callback) {
		request('post','/v1api/miniapp.user/login',params,callback);
	},
	register: function(params,callback) {
		request('post','/v1api/miniapp.user/register',params,callback);
	},
	getMemberBanlance: function(params,callback) {
		return request('get', '/v1api/miniapp.member.balance/get',params,callback)
	},
	getAreaInfo: function(params,callback) {
		return request('post', '/groupapi/region',params,callback)
	},
	getGoodsBanner: function(params,callback) {
		return request('post', '/v1api/miniapp.banner/get',params,callback)
	},
	getInstitutionList: function(params,callback) {
		return request('post','/v1api/miniapp.mechanism/get',params,callback);
	},
	getGoodsList: function(params,callback) {
		return request('post', '/v1api/miniapp.goods/getList',params,callback)
	},
	getGoodsInfo: function(params,callback) {
		return request('post', '/v1api/miniapp.goods/get',params,callback)
	},
	creatOrder: function(params,callback) {
		return request('post', '/v1api/miniapp.order/create',params,callback)
	},
	getOrderList: function(params,callback) {
		return request('get', '/v1api/miniapp.order/getList',params,callback)
	},
	getOrderDetail: function(params,callback) {
		return request('post', '/v1api/miniapp.order/get',params,callback)
	},
	getAuntInfo: function(params,callback) {
		return request('get', '/v1api/miniapp.aunt/get',params,callback)
	},
	commentAunt: function(params,callback) {
		return request('get', '/v1api/miniapp.member.card.order/comment',params,callback)
	},
	payOrder: function(params,callback) {
		return request('post', '/v1api/miniapp.payment/pay',params,callback)
	},
	payCardOrder: function(params,callback) {
		return request('post', '/v1api/miniapp.payment/cardPay',params,callback)
	},
	getRegionList: function(params,callback) {
		return request('post', '/v1api/miniapp.common/getRegionList',params,callback)
	},
	updateNowRegion: function(params, callback) {
		return request('post', '/v1api/miniapp.user/updateRegion', params, callback)
	},
	
	getAddressList: function(params,callback) {
		return request('get', '/v1api/miniapp.member.address/getList',params,callback)
	},
	addAddress: function(params,callback) {
		return request('post', '/v1api/miniapp.member.address/create', params, callback)
	},
	updateAddress: function(params,callback) {
		return request('post', '/v1api/miniapp.member.address/update', params, callback)
	},
	deleteAddress: function(params,callback) {
		return request('post', '/v1api/miniapp.member.address/delete', params, callback)
	},
	setDefaultAddress: function(params,callback) {
		return request('post', '/v1api/miniapp.member.address/setDefault', params, callback)
	},
	
	getServerOrder: function(params,callback) {
		return request('get', '/v1api/miniapp.member.card.order/getList', params, callback)
	},
	
	getQrCode: function(params,callback) {
		return request('post', '/v1api/miniapp.user/getQrCode', params, callback)
	},
	getUserhetong: function(params,callback) {
		return request('post', '/v1api/miniapp.user/getUserhetong', params, callback)
	},
	getMyFortuneStar: function(params,callback) {
		return request('post', '/v1api/miniapp.user/getMyFortuneStar', params, callback)
	},
	hetongDetail: function(params,callback) {
		return request('post', '/v1api/auntapp.Order/hetongDetail', params, callback)
	},
	ayihetong: function(params,callback) { //合同预约
		return request('post', '/v1api/auntapp.Order/ayihetong', params, callback)
	},
	getCat: function(params,callback) { //合同分类
		return request('post', '/v1api/miniapp.order/getCat', params, callback)
	},
	orderYuYue: function(params,callback) { //合同分类
		return request('post', '/v1api/miniapp.order/yuYue', params, callback)
	},
	getCateList:  function(params,callback) {
		return request('post', '/v1api/miniapp.goods/category', params, callback)
	},
	getGoodsList:  function(params,callback) {
		return request('post', '/v1api/miniapp.goods/getGoodsByCategory ', params, callback)
	},
	getCardList:  function(params,callback) {
		return request('post', '/v1api/miniapp.user/memberCard', params, callback)
	},
	getOrderCardList:  function(params,callback) {
		return request('post', '/v1api/miniapp.user/getMemberCardByOrder', params, callback)
	},
	
	
	
	
};
let custom={
	/**自定义函数*/
	mb_str:function(res,len,cutlen){
		if(res.length>len) res = res.substring(0,cutlen)+'...';  
		return res;
	},
	radian:function(d){
		return d*3.1415926535898 / 180.0;
	},
	// 判断距离
	getDistance: function(lng1,lat1,lng2,lat2){
		let radLat1 = lat1*3.1415926535898 / 180.0;
		let radLat2 = lat2*3.1415926535898 / 180.0; 
		let a = (lat1*3.1415926535898 / 180.0) - (lat2*3.1415926535898 / 180.0);
		let b = (lng1*3.1415926535898 / 180.0) - (lng2*3.1415926535898 / 180.0); 
		let s = 2 * Math.asin ( Math.sqrt ( Math.pow ( Math.sin ( a / 2 ), 2 ) + Math.cos ( radLat1 ) * 
		Math.cos ( radLat2 ) * Math.pow ( Math.sin ( b / 2 ), 2 ) ) ); 
		s = s * 6378.137; //乘上地球半径，单位为公里(km) 
		//console.log(s);  
		return s.toFixed(2);
	}
};

function request(method,url,params,callback){
	// 获取登录Token
	let header = {};
	let token = uni.getStorageSync('access_token');
	if(token) header = {'Token':token};
	// 请求数据
	uni.request({
		url: domain+url, 
		method:method,
		data:params, 
		header:header,
		success: (res) => {
			if(res.statusCode === 200){
				if(res.data.code != 200 && res.data.code != 0  ){
					uni.showToast({
						icon: 'none',
						title:res.data.msg,
					})
				}
			} return callback(res.data);
			callback({code:0,msg:'网络或服务器异常'});
		},
		error: (res) => {
			callback({code:0,msg:'网络或服务器异常'});
		}
	});
}

module.exports = {
	domain: domain,
	api: api,
	custom:custom
}